<?php
/**
 * User: 苏翔
 * Date: 2024/8/16 14:07
 */

namespace App\Dao;

use App\FundConf;
use App\Model\PriceDailyModel;
use App\Model\PriceHourlyModel;
use App\Model\PriceWeeklyModel;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

class PriceWeeklyDao extends BaseDao
{

    #[Inject]
    protected PriceWeeklyModel $model;
    #[Inject]
    protected PriceDailyModel $logModel;

    function getList($uid = 0, $category = 0, $min_id = 0, $max_id = 0, $size = 10): array
    {
        $size = max(1, (int)$size);
        $where = compact('uid', 'category');
        $where = array_filter($where);
        $orderDirection = 'desc';
        if ($min_id) {
            $where[] = ['id', '>', $min_id];
            $orderDirection = 'asc';
        } elseif ($max_id) {
            $where[] = ['id', '<', $max_id];
        }
        return $this->model->where($where)->orderBy('id', $orderDirection)->limit($size)->get()->toArray();
    }


    function lastLog(): array
    {
        // 读id最大一条，add_time<$add_time
        $rs = $this->logModel->orderBy('id', 'desc')->first();
        return $rs ? $rs->toArray() : [];
    }

    function getStats($start_time, $ceil_time)
    {
        // 得到log 开始结束时间 high low
        $rs = DB::table($this->logModel->getTable())
            ->whereBetween('key_date', [$start_time, $ceil_time])
            ->selectRaw('min(lowest) as lowest, max(highest) as highest')
            ->get();
        $rs = $rs->toArray()[0];
        $rt = [];
        if ($rs) {
            $rt['lowest'] = $rs->lowest;
            $rt['highest'] = $rs->highest;
        }
        return $rt;
    }


    function exist($key)
    {
        return $this->model->where('key_date', $key)->value('id');
    }

    function handle($date)
    {
        [$start_date, $end_date] = $this->parseStartEndDateOfThisWeek($date);
        $key_date = $start_date;
        $lastLog = $this->lastLog();

        $close = $lastLog['close'] ?? 0;
        if (!($close > 0)) {
            echo "lastLog is empty";
            return false;
        }
        if (!$this->exist($key_date)) {
            return $this->add($key_date, $end_date, $lastLog);
        }
        $data = $this->getStats($start_date, $end_date);

        if (!$data) {
            echo "getStats is empty";
            return false;
        }
        $data['close'] = $close;
        return $this->model->where('key_date', $key_date)->update($data);
    }


    function parseStartEndDateOfThisWeek($date)
    {
        $start_date = $end_date = '';
        if ($date) {
            $date = strtotime($date);
            $week = date('w', $date);
            $week = $week == 0 ? 7 : $week;
            $start_date = date('Y-m-d', $date - ($week - 1) * 86400);
            $end_date = date('Y-m-d', $date + (7 - $week) * 86400);
        }
        return [$start_date, $end_date];
    }

    function add($time_key, $end_date, $log)
    {

        $open = $log['open'] ?? 0;
        $close = $log['close'] ?? 0;
        if (!$open) {
            $open = $close;
        }

        if (!($close > 0)) {
            return false;
        }

        $data = [
            'key_date' => $time_key,
            'end_date' => $end_date,
            'close' => $close,
            'open' => $open,
            'highest' => $close,
            'lowest' => $open,
            'add_time' => time(),
        ];

        return $this->model->insertGetId($data);
    }

}
